<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>websocket客户端</title>

        <script type="text/javascript">
            var socket;
            if (!window.WebSocket){
                console.log("window.WebSocket is null");
            }
            if (window.WebSocket){
                socket = new WebSocket("ws://localhost:8888/websocket");
                socket.onmessage = function (ev) {
                    var ta = document.getElementById("responseContent");
                    ta.innerText += ev.data +"\r\n";
                };

                socket.onopen = function (ev) {
                    var ta = document.getElementById("responseContent");
                    ta.innerText = "您的浏览器支持websocket，请进行后续操作"
                }

                socket.onclose = function (ev) {
                    var ta = document.getElementById("responseContent");
                    ta.innerText = ""
                    ta.innerText = "WebSocket连接已关闭"
                }

                function send(message) {
                    if (!window.WebSocket){
                        return;
                    }
                    if (socket.readyState == WebSocket.OPEN){
                        socket.send(message);
                    }else {
                        alert("WebSocket连接没有建立成功")
                    }
                }
            }else {
                alert("您的浏览器不支持websocket")
            }
        </script>
    </head>

    <body>
        <form onsubmit="return false;">
            <input type="text" name="message" value=""/>
            <br/><br/>
            <input type="button" value="发送websocket请求消息" onclick="send(this.form.innerText)"/>

            <hr color="red"/>
            <h2>客户端接收服务端返回的应答消息</h2>

            <textarea id="responseContent" style="width: 1024px;height: 300px"></textarea>
        </form>
    </body>
</html>